package web.servlet;

import domain.Account;
import service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "/RecoverServlet", urlPatterns = {"/Recover"})
public class RecoverServlet extends HttpServlet {
    private AccountService accountService = new AccountService();
    private String userid, phone, checkcode, captcha, recoverInfo;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        userid = req.getParameter("userid");
        phone = req.getParameter("phone");
        checkcode = req.getParameter("checkcode");
        Account user = accountService.findUserByIdAndPhone(userid,phone);
        if(CaptchaServlet.checkCodeCorrect(checkcode)){
            if(user != null){
                req.getSession().setAttribute("resetUserid", user.getUserid());
                req.getRequestDispatcher("/ResetForm").forward(req,resp);
            }else {
                req.setAttribute("recoverInfo", "用户名或手机号码错误");
                req.getRequestDispatcher("/RecoverForm").forward(req,resp);
            }
        }else {
            req.setAttribute("recoverInfo", "验证码错误");
            req.getRequestDispatcher("/RecoverForm").forward(req,resp);
        }
    }
}
